<template>
	<view class="content" >
		<view class="head-view">
			<view style="margin-left: 32rpx;color: #333333;">头像</view>
			<button class='avatar-view' open-type="chooseAvatar" @chooseavatar="onChooseavatar">
				<image class="avatar-image" :src="avatar"></image>
			</button>
			<image class="arrow-image" src="../../static/mine/ic_arrow.png"></image>
		</view>
		<view class="nickName-view" >
			<view style="margin-left: 32rpx;color: #333333;">昵称</view>
			<input class="input-box-view" maxlength="20" type="nickname" @blur="blurname" v-model="nickName" placeholder="请输入昵称" placeholder-class="placeholder-view"/>
			<image class="arrow-image" src="../../static/mine/ic_arrow.png"></image>
		</view>
		<view @tap="saveInfoClick()" class="logout-btn">保存</view>
	</view>
</template>

<script>
	import configUrls from '@/common/config.js';
	export default {
		data() {
			return {
				avatar:"",
				nickName:"微信用户",
				uploadUrl: "",
				uploadNick: "",
			}
		},
		onLoad() {
	        const userInfoTemp = uni.getStorageSync("userInfo");
			if (userInfoTemp) {
				this.avatar = userInfoTemp.avatar;
				this.nickName = userInfoTemp.nickName;
			}
		},
		methods: {
			saveInfoClick() {
				if (this.uploadUrl) {
					this.getQiniuToken();
				} else if (this.uploadNick) {
					this.updateUserInfo(null);
				} else {
					uni.navigateBack({
						delta: 1
					})
				}
			},
			async onChooseavatar(e) {
				this.avatar = e.detail.avatarUrl;
				this.uploadUrl = e.detail.avatarUrl;
			},
			getQiniuToken() {
				let that = this;
				this.$Request.postJsonLoadView(configUrls.getQiniuToken, {
				}).then(res => {
					if (res.code == "10000") {
						that.uploadFile(res.data.url, res.data.token);
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					} 
				}); 
			},
			uploadFile(url, token) {
				let that = this;
				let a = uni.uploadFile({
					url: url,
					filePath: this.avatar,
					name:"file",
					formData:{
						token: token
					},
					success: (res) => {
						const respData = JSON.parse(res.data);
						that.updateUserInfo(respData.key);
					}
				})
			},
			blurname(e) {
			    this.nickName = e.detail.value;
				this.uploadNick = e.detail.value;
			},
			updateUserInfo(key) {
				let that = this;
				let reqParams = {needDomain: true};
				if (key) {
					reqParams['avatar'] = key;
				}
				if (that.uploadNick) {
					reqParams['name'] = that.uploadNick;
				}
				this.$Request.postJsonLoadView(configUrls.updateUserInfo, reqParams).then(res => {
					if (res.code == "10000") {
						uni.navigateBack({
							delta:1,
						})
					} 
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 2000
					});
				}); 
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		background-color: #F5F5F5;
		width: 100vw;
		height: 100vh;
	}
	.head-view{
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #FFFFFF;
		margin-top: 20rpx;
		width: 100vw;
		height: 137rpx;
		font-size: 28rpx;
	} 
	.avatar-view {
		display:flex;
		align-items:center;
		justify-content:center;
		background: rgba(255, 255, 255, 0);
		margin-right: 0.1rpx;
		padding-left: 16rpx;
		padding-right: 16rpx;
	}
	.avatar-image {
		width: 90rpx;
		height: 90rpx;
		border-radius: 50%;
	}
	.arrow-image {
		width: 32rpx;
		height: 32rpx;
		margin-right: 32rpx;
	}
	button::after{border:0}
	.nickName-view{
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: #FFFFFF;
		margin-top: 1rpx;
		width: 100vw;
		height: 91rpx;
		font-size: 28rpx;
	}
	.input-box-view {
		flex: 1;
		font-size: 28rpx;
		font-family: AppleSystemUIFont;
		color: #666666;
		margin-right: 16rpx;
		margin-left: 80rpx;
		text-align: right;
	}
	.placeholder-view {
		color: #ccc; 
	}
	.logout-btn{
		margin-left: 50rpx;
		margin-right: 50rpx;
		margin-top: 160rpx;
		background: linear-gradient(138deg, #F7CB47 0%, #D4CE4E 100%);
		border-radius: 88rpx;
		line-height: 88rpx;
		text-align: center;
		font-size: 32rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
	}
</style>
